//
//  PayOne.m
//  xjairOA
//
//  Created by apple on 16-1-5.
//  Copyright (c) 2016年 net. All rights reserved.
//

#import "PayOne.h"
#import "Constants.h"
#import "AppDelegate.h"
#import "PayDetail.h"

@interface PayOne ()

@end

@implementation PayOne
NSMutableDictionary * tableDataDictionary;
NSArray * tableData;
//NSArray * stories;
NSMutableArray * months;
AppDelegate * appDelegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"工资条";
    self.table.dataSource = self;
    self.table.delegate = self;
    months = [[NSMutableArray alloc] init];
    tableDataDictionary = [[NSMutableDictionary alloc] init];
//    tableData = [NSDictionary dictionaryWithObjectsAndKeys:
//                 [NSArray arrayWithObjects:@"孙悟空",@"猪八戒",@"牛魔王",@"蜘蛛精",@"唐僧",@"沙和尚", nil],@"西游记",
//                 [NSArray arrayWithObjects:@"宝玉",@"黛玉",@"元春",@"探春",@"惜春",@"可卿", nil],@"红楼梦",
//                 [NSArray arrayWithObjects:@"武松",@"林冲",@"鲁达",@"杨志",@"宋江",@"史进", nil],@"水浒",
//                 [NSArray arrayWithObjects:@"关羽",@"刘备",@"张飞",@"曹操",@"张辽",@"吕布", nil],@"三国演义",
//                 nil];
//    stories = [[tableData allKeys] sortedArrayUsingSelector:@selector(compare:)];
    
    appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.manager
     GET:payaction
     parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         tableData = responseObject;
         //[tableData addObjectsFromArray:responseObject];
        
         //         NSDictionary* dict = [notices objectAtIndex:0];
         //         self.biaoti.text = [dict objectForKey:@"title"];
         //         self.neirong.text = [dict objectForKey:@"desc"];
         for (id object in tableData) {
             NSDictionary * tempDictionary = object;
             [months addObject:[tempDictionary objectForKey:@"time"]];
             [tableDataDictionary setObject:[NSArray arrayWithObjects:[tempDictionary objectForKey:@"id"],[tempDictionary objectForKey:@"shouldProvide"],[tempDictionary objectForKey:@"reduceTotal"],[tempDictionary objectForKey:@"factProvide"],nil]  forKey:[tempDictionary objectForKey:@"time"]];
         }
          [self.table reloadData];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"获取服务器响应出错！");
     }];
   
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return months.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //NSString * story = [months objectAtIndex:section];
//    return [[tableData objectForKey:story] count];
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger  sectionNo = indexPath.section;
   // NSUInteger rowNo = indexPath.row;
    NSString * month = [months objectAtIndex:sectionNo];
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"payonecell" forIndexPath:indexPath];
    cell.layer.cornerRadius=12;
    cell.layer.masksToBounds=YES;
    UILabel * label1 = (UILabel*)[cell viewWithTag:1];
    UILabel * label2 = (UILabel*)[cell viewWithTag:2];
    UILabel * label3 = (UILabel*)[cell viewWithTag:3];
//    label.text = [[tableDataDictionary objectForKey:month] objectAtIndex:rowNo];
//    NSLog(@"%@",[[tableDataDictionary objectForKey:month] objectAtIndex:1]);
    label1.text = [[tableDataDictionary objectForKey:month] objectAtIndex:1];
    label2.text = [[tableDataDictionary objectForKey:month] objectAtIndex:2];
    label3.text = [[tableDataDictionary objectForKey:month] objectAtIndex:3];
    return cell;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return months;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [months objectAtIndex:section];
}

//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
//{
////    NSString * month = [months objectAtIndex:section];
////    return [NSString stringWithFormat:@"一共%lu个人物",(unsigned long)[[tableData objectForKey:month] count]];
//    return @"end";
//}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UITableViewCell * cell = (UITableViewCell*)sender;
    NSIndexPath * indexPath = [self.table indexPathForCell:cell];
    PayDetail * payDetail = (PayDetail*)segue.destinationViewController;
    payDetail.payId = [[tableData objectAtIndex:indexPath.section] objectForKey:@"id"];
    payDetail.month = [[tableData objectAtIndex:indexPath.section] objectForKey:@"time"];
    //NSLog(@"%@",[[tableData objectAtIndex:indexPath.section] objectForKey:@"id"]);
    //NSLog(@"%@",[[tableData objectAtIndex:indexPath.section] objectForKey:@"time"]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
